// base
import Taro, { Component } from '@tarojs/taro'
import { View, Text } from '@tarojs/components'
import { connect } from '@tarojs/redux'
import { UPDATEPAGECHOOSEJOBINPUT } from '../../../../pages/choose-job-input/store/types/pagechoosejobinput'
import { UPDATEUSERINFO } from '../../../../store/types/userinfo'
// less
import './index.less'
import ListItem from '../../../../components/page-components/list-item'
import { navigateTo } from '../../../../crossplatform/apiservice/navigate'
import pagejumplist from '../../../../configuration/pagejumplist.json'
import { getUserArchiveInfoRequest } from '../../../../service/user-archive-controller'
import formatdata from '../../../../util/pagemethods/userArchInfo'
import { deepCopy } from '../../../../util/lodashmin'
import { showToast } from '../../../../crossplatform/apiservice/toast'
import { getArchiveInfoRequest } from '../../../../service/user-info-controller'
import { judgeLoginStatus } from '../../../../util/login_status'

const mapActiontoProps = dispatch => ({
  updateJobInput(payload) {
    dispatch({
      type: UPDATEPAGECHOOSEJOBINPUT,
      payload: {
        ...payload
      }
    })
  },
  updateUserState(payload) {
    dispatch({
      type: UPDATEUSERINFO,
      payload: {
        ...payload
      }
    })
  }
})
@connect(
  ({ userinfo }) => ({
    ...userinfo
  }),
  mapActiontoProps
)
class PersonalInfo extends Component {
  constructor(props) {
    super(props)
    this.state = {
      infoList: [
        {
          name: '求职意向',
          icon: 'https://imagesize.hrwork.com/sp/files/ecd36295-8076-417d-890e-2936e3969a90.png',
          url: 'new-job-intention',
          desc: '请选择'
        },
        {
          name: '自我评价',
          icon: 'https://imagesize.hrwork.com/sp/files/2888fc55-997b-4be3-bb6a-02ac3529812c.png',
          input: true,
          num: 0,
          desc: '去完善'
        },
        {
          name: '教育信息',
          icon: 'https://imagesize.hrwork.com/sp/files/c3dd0f67-9c65-48ff-96ca-04d3d862a7c3.png',
          url: 'new-educationa-information',
          num: 0,
          desc: '去完善'
        },
        {
          name: '工作经验',
          icon: 'https://imagesize.hrwork.com/sp/files/937c7c03-39b0-4f67-b515-afa1f3f4c1d9.png',
          url: 'new-work-exp',
          num: 0,
          desc: '去完善'
        },
        {
          name: '我的相册',
          icon: 'https://imagesize.hrwork.com/sp/files/bfcbffae-208e-4f82-8817-13a32b293ea4.png',
          url: 'my-album',
          image: [],
          desc: '去上传'
        }
      ],
      dataformat: {},
      archiveId: ''
    }
  }

  componentWillMount() {
    // onLoad
  }

  componentDidMount() {
    // onready
  }
  componentDidShow() {
    // onshow
    const { userId } = this.props
    if (userId) {
      this.getData(userId)
    }
  }

  componentWillUnmount() {
    // onUnload
  }

  componentWillReceiveProps(nextprops) {}

  clickPreview = () => {
    // 点击预览
  }

  getData = userId => {
    getArchiveInfoRequest({ userId }).then(({ data: dataUser }) => {
      getUserArchiveInfoRequest({ userId }).then(({ data }) => {
        const { networkerror, entry, status, message } = data
        if (networkerror) {
          // resolve({
          //   networkerror: true
          // })
        } else {
          let Formatdata = new formatdata(entry || {}, '')
          let dataformat = Formatdata.getFormatData()
          let infoListCopy = deepCopy(this.state.infoList)
          infoListCopy[0].desc =
            dataUser && dataUser.entry && dataUser.entry.jobStatus == 2 ? '考虑机会' : '寻找工作'
          this.props.onUpdateJobStatus(infoListCopy[0].desc)

          if (
            dataformat.userInfo &&
            dataformat.userInfo.intro &&
            dataformat.userInfo.intro.length
          ) {
            infoListCopy[1].num = dataformat.userInfo.intro.length
            infoListCopy[1].desc = '字'
          } else {
            infoListCopy[1].num = 0
            infoListCopy[1].desc = '去完善'
          }

          if (dataformat.educationInformation && dataformat.educationInformation.length) {
            infoListCopy[2].num = dataformat.educationInformation.length
            infoListCopy[2].desc = '所学校'
          } else {
            infoListCopy[2].num = 0
            infoListCopy[2].desc = '去完善'
          }

          if (dataformat.workExp && dataformat.workExp.length) {
            infoListCopy[3].num = dataformat.workExp.length
            infoListCopy[3].desc = '份工作'
          } else {
            infoListCopy[3].num = 0
            infoListCopy[3].desc = '去完善'
          }

          infoListCopy[4].image =
            dataformat.elegantDemeanour && dataformat.elegantDemeanour.length
              ? dataformat.elegantDemeanour
              : []

          this.setState({
            infoList: infoListCopy,
            dataformat,
            archiveId: (dataUser.entry && dataUser.entry.archiveId) || ''
          })
          this.props.updateUserState({
            archiveComplete: (dataUser.entry && dataUser.entry.archiveComplete) || ''
          })
        }
      })
    })
  }

  inputJumpTo = () => {
    // 先获取存储的自我评价/ 再跳转页面
    const {
      dataformat: { userInfo = {} },
      archiveId
    } = this.state
    const { intro } = userInfo
    this.props.updateJobInput({
      inputData: {
        type: 'text',
        value: intro,
        name: '自我评价',
        componenttype: 'textarea',
        maxLength: 140,
        tip: '*充分展现自我优势，让更多企业发现你'
      }
    })

    judgeLoginStatus({ url: 'choose-job-input', parmas: `self=1&archiveId=${archiveId}` })
  }

  onResumePreview = () => {
    const {
      userInfoC: { mobile, userName }
    } = this.props
    if (!mobile || !userName) {
      showToast({
        title: '请完善个人信息'
      })
      return
    }
    const { infoList } = this.state
    if (infoList[0].desc === '请选择') {
      showToast({
        title: '请完善求职意向'
      })
      return
    }
    if (infoList[2].desc === '去完善' && infoList[3].desc === '去完善') {
      showToast({
        title: '请完善求教育信息'
      })
      return
    }
    navigateTo({
      url: `${pagejumplist['my-resume'].path}?self=1`
    })
  }

  render() {
    const { infoList } = this.state
    const { archiveComplete } = this.props
    return (
      <View className="PersonalInfo_id28155">
        <View className="info-container">
          <View className="info-title">
            <View className="info-top">迈进职场第一步</View>
            <View className="info-main">
              <View className="info-main-title">我的简历</View>
              <View className="info-complete">
                / 完善度
                <View className="info-complete-persent">
                  {archiveComplete > 1 ? 100 : parseInt(archiveComplete * 100)}%
                </View>
              </View>
            </View>
            <View className="preview" onClick={this.onResumePreview}>
              预览
            </View>
          </View>
          <View className="list">
            {infoList.map((item, index) => {
              return item.name == '我的相册' ? (
                <ListItem
                  name={item.name}
                  type="image"
                  icon={item.icon}
                  image={item.image}
                  desc={item.desc}
                  userId={this.props.userId}
                  url={item.url}
                />
              ) : item.input ? (
                <ListItem
                  name={item.name}
                  num={item.num}
                  icon={item.icon}
                  desc={item.desc}
                  input={item.input}
                  onTap={this.inputJumpTo}
                  userId={this.props.userId}
                />
              ) : (
                <ListItem
                  name={item.name}
                  num={item.num}
                  icon={item.icon}
                  desc={item.desc}
                  url={item.url}
                  userId={this.props.userId}
                />
              )
            })}
          </View>
        </View>
      </View>
    )
  }
}

export default PersonalInfo
